package tk.jfree.example.zonedcode;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import tk.jfree.example.zonedcode.model.ZonedCode;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Cheng.Wei
 */
public class ZonedCodeApplication {

    private static final String URL = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/";

    public static Document get(String url) throws IOException {
        return Jsoup.parse(new URL(url).openStream(), "gbk", url);
    }

    public static List<ZonedCode> index(Category className, String uri) throws IOException{

        return get(URL+uri).getElementsByClass(className.val).select("td").select("a")
                .stream()
                .map(
                        e->
                                ZonedCode.builder()
                                        .id(e.attr("href").trim().replaceAll(".html", "0000000000"))
                                        .name(Jsoup.parse(e.html()).text())
                                        .uri(e.attr("href").trim())
                                        .build())
                .collect(Collectors.toList());
    }

    public static List<ZonedCode> other(Category className, String uri) throws IOException{

        return get(URL+uri).getElementsByClass(className.val)
                .stream()
                .map(
                        x->{
                            Elements elements=null;
                            for (String rule: className.rule){
                                elements = x.select(rule);
                            }
                            Element[] e =  elements.stream().toArray(Element[]::new);
                            return ZonedCode.builder()
                                    .id(Jsoup.parse(e[0].html()).text())
                                    .name(Jsoup.parse(e[className.i].html()).text())
                                    .uri(uri.substring(uri.lastIndexOf("/")+1, uri.indexOf(".html")).replaceAll("(.{2})","$1/")
                                            +e[className.i].select("a").attr("href")
                                            .substring(e[className.i].select("a").attr("href").lastIndexOf("/")+1)).build();
                        })
                .collect(Collectors.toList());
    }


    public static void main(String[] args) throws IOException{
        System.out.println("===== province ========");
        index(Category.PROVINCE, "index.html").stream().forEach(System.out::println);

        System.out.println("===== city [北京]========");
        other(Category.CITY, "11.html").stream().forEach(System.out::println);

        System.out.println("===== county [市辖区]========");
        other(Category.COUNTY, "11/1101.html").stream().forEach(System.out::println);

        System.out.println("===== town [东城区]========");
        other(Category.TOWN, "11/01/110101.html").stream().forEach(System.out::println);

        System.out.println("===== village [东直门街道办事处]========");
        other(Category.VILLAGE, "11/01/01/110101009.html").stream().forEach(System.out::println);
    }

    enum Category{
        PROVINCE("provincetr", new String[]{"td", "a"}, 0),
        CITY("citytr", new String[]{"tr", "a"}, 1),
        COUNTY("countytr", new String[]{"tr", "td"} ,1),
        TOWN("towntr", new String[]{"tr", "a"}, 1) ,
        VILLAGE("villagetr", new String[]{"tr", "td"}, 2);
        final String val;
        final String[] rule;
        final int i;
        Category(String val, String[] rule, int i){
            this.val = val;
            this.rule = rule;
            this.i = i;
        }
    }
}
